//
//  PPProfileTableViewCommentCell.m
//  pickpic
//
//  Created by Jinxiao on 10/5/16.
//  Copyright © 2016 MicroDream. All rights reserved.
//

#import "PPProfileTableViewCommentCell.h"

#import "DGCoreTextView.h"

#define insets UIEdgeInsetsMake(0, 15, 0, 15)

@interface PPProfileTableViewCommentCell ()

@property (readwrite, nonatomic, strong) DGCoreTextView *textView;

@end

@implementation PPProfileTableViewCommentCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    _textView = [[DGCoreTextView alloc] init];
    _textView.backgroundColor = [UIColor clearColor];
    [self addSubview:_textView];

    return self;
}

- (void)setComment:(PPTimelineCommentObject *)comment {
    _comment = comment;

    _textView.editor = comment.editor;

    [self setNeedsLayout];
}

- (void)layoutSubviews {
    [super layoutSubviews];

    CGFloat contentWidth = self.width - insets.left - insets.right;

    CGSize textSize = [_comment.editor sizeThatFits:CGSizeMake(contentWidth, self.height)];
    _textView.frame = CGRectMake(insets.left, insets.top, textSize.width, textSize.height);
}

+ (CGFloat)heightForComment:(PPTimelineCommentObject *)comment constrainedSize:(CGSize)constrainedSize {
    CGFloat height = insets.top + insets.bottom;

    CGSize contentSize = CGSizeMake(constrainedSize.width - insets.left - insets.right, constrainedSize.height);
    height += [comment.editor sizeThatFits:contentSize].height;
    
    return height;
}

@end
